package com.mrzhou.thread.study;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;

/**
 * P24 线程常用方法Sleep_可读性
 *  使用TimeUnit的sleep代替Thread.sleep
 * @author qiang.zhou
 */
@Slf4j
public class P25ThreadSleepReadable {

    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread(() -> {
            log.info("start sleep....");
            try {
                // 当前线程睡眠2秒等同于 Thread.sleep(2000)
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                log.error("interrupted 线程休眠被中断了...", e);
            }
            log.info("end sleep...");
        }, "t1");
        t.start();
        // main线程休眠1秒 Thread.sleep(1000)
        TimeUnit.SECONDS.sleep(1);
        log.info("interrupt....");
        // 中断t1线程
        t.interrupt();
    }

}
